﻿using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;

namespace Com.OpenCode.Framework.DevForm
{
    public class StatusButtonUI : BarButtonItem, IStatusSetValue
    {
        private ICommand _Command;

        public StatusButtonUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            Caption = args.Plugin.Properties.GetString("Text");
            Width = args.Plugin.Properties.GetInt("Width");
            Plugin commandPlugin = args.Plugin.Addin.Plugin.Search("Class", "Id", args.Plugin.Properties.GetString("Image"));
            if (commandPlugin != null)
            {
                _Command = (ICommand)args.Plugin.Addin.Bulid(commandPlugin, null);
            }
            (args.Owner as StatusBarUI).ItemLinks.Add(this);
            ItemClick += StatusButtonUI_ItemClick;
        }

        public override bool Enabled
        {
            get
            {
                if (_Command == null)
                {
                    return false;
                }
                return _Command.Enable();
            }
            set { base.Enabled = value; }
        }

        private void StatusButtonUI_ItemClick(object sender, ItemClickEventArgs e)
        {
            _Command.Run();
        }

        #region IStatusSetValue 成员

        public int InformationIndex
        {
            get { return -1; }
        }

        public void SetStatusValue(object value)
        {
        }

        public void SetStatusVisible(bool visible)
        {
        }

        #endregion
    }
}